﻿function ShowMsg(thisObj, strMsg) {
    if (!thisObj)
        return;

    thisObj.attr("err", "err");
    var smal = thisObj.next("small");
    if (0 < smal.length) {
        $(smal[0]).text(strMsg).show();
    }
    else {
        thisObj.after($("<small></small>").text(strMsg).css({ color: "red" }));
    }
}

function ShowMsgAppendTo(thisObj, strMsg) {
    if (!thisObj)
        return;

    var smal = thisObj.children("small");
    if (0 < smal.length) {
        $(smal[0]).text(strMsg).show();
    }
    else {
        thisObj.appendTo($("<small></small>").text(strMsg).css({ color: "red" }));
    }
}

function ClearMsg(thisObj) {
    if (!thisObj)
        return;

    thisObj.removeAttr("err");
    var smal = thisObj.next("small");
    if (0 < smal.length) {
        $(smal[0]).hide();
    }
}

var phoneLast;
$("#useraccount").blur(function () {
    ClearMsg($("#useraccount"));
    var account = $("#useraccount").val();
    if (account.length < 4) {
        ShowMsg($("#useraccount"), "用户账号不能少于4个字符");
        return;
    }

    if (!/^[A-Za-z0-9_]*$/.test(account)) {
        ShowMsg($("#useraccount"), "仅支持字母,数字和下划线");
        return;
    }

    $.ajax({
        type: 'get',
        url: '/Register.ashx?action=GetPhone&account=' + account,
        dataType: 'text',
        success: function (e) {
            if (-1 < e.indexOf("ok")) {
                phoneLast = e.split('_')[1];
            }
            else
                ShowMsg($("#useraccount"), e);
        }
    });

});

$("#userpwd").blur(function () {
    var userpwd = $("#userpwd").val();
    ClearMsg($("#userpwd"));
    if (userpwd.trim() == "") {
        ShowMsg($("#userpwd"), "密码不能为空");
    }
});

$("#checkuserpwd").blur(function () {
    var userpwd = $("#checkuserpwd").val();
    ClearMsg($("#checkuserpwd"));
    if (userpwd.trim() == "") {
        ShowMsg($("#checkuserpwd"), "密码不能为空");
    }
    else {
        var userpwdff = $("#userpwd").val();
        if (userpwdff.trim() != "") {
            if (userpwdff != userpwd)
                ShowMsg($("#checkuserpwd"), "两次输入的密码不一致");
        }
    }
});

$("#phoneCheck").blur(function (e) {
    $(".am-g .cheCode small").hide();
    $("#phoneCheck").removeAttr("err");
    if (!/^\d{6}$/.test($("#phoneCheck").val())) {
        $("#phoneCheck").attr("err", "err");
        $(".am-g .cheCode small").show();
    }
});

function checkForm() {
    var list = $('.am-form-group input[err="err"]');

    if (0 != list.length) {
        alert("请输入有效信息!");
        return false;
    }
    else
        return true;
}

function OnClickGetCode() {
    var phone = $("#userPhone");
    if (phone.attr("err")) {
        alert("请输入正确手机号码");
        return;
    }

    if ((/^1[34578]\d{9}$/.test(phone.val()))) {
        var nTime = 60;
        //取消点击响应, 避免重复点击
        $("#sendCode").attr("onclick", "");
        $.post("/PostMsg.aspx", "posttype=getcodeforforgetpsw&phoneNum=" + phone.val(), function (e) {
            if ("ok" == e) {
                //按钮不可用, 并进入60秒倒计时
                $("#sendCode").attr("disabled", true);
                var timeTer = setInterval(function (e) {
                    --nTime;
                    $("#sendCode").text("已发送! (" + nTime + " s)");
                    if (0 == nTime) {
                        clearInterval(timeTer);
                        $("#sendCode").attr("disabled", false);
                        $("#sendCode").attr("onclick", "OnClickGetCode()");
                        $("#sendCode").text("获取验证码");
                    }
                }, 1000);
            }
            else {
                alert(e);
                $("#sendCode").attr("onclick", "OnClickGetCode()");
            }
        });
    }
    else {
        alert("输入有效手机号码");
    }
}

$("#userPhone").blur(function (e) {
    var phone = $(this);
    ClearMsg(phone);
    if ((/^1[34578]\d{9}$/.test(phone.val()))) {
        if (phoneLast) {
            if (-1 == phone.val().indexOf(phoneLast)) {
                ShowMsg(phone, "该号码与注册时的号码不一致");
            }
        }
        else {
            ShowMsg(phone, "请先输入帐号!");
        }
    }
    else {
        ShowMsg(phone, "请输入有效手机号码");
    }
});
